package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IProductGropMemberService;
import com.wondertek.poms.core.service.IProductGroupService;
import com.wondertek.poms.dao.po.ProductGropMember;
import com.wondertek.poms.dao.po.ProductGroup;
import com.wondertek.poms.dao.repo.IProductGropMemberDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ProductGropMemberServiceImpl extends ServiceImpl<ProductGropMember,Long> implements IProductGropMemberService {

    @Autowired
    private IProductGropMemberDao productGropMemberDao;
    @Autowired
    private IProductGroupService productGroupService;

    @Override
    public ProductGropMember deleteLogic(Long id) {
        return null;
    }

    @Override
    public ProductGropMember merge(ProductGropMember model) {
        return null;
    }

    @Override
    @Transactional
    public void deleteByGroupId(Long groupId) {
        productGropMemberDao.deleteByGroupId(groupId);
    }

    @Override
    public List<ProductGropMember> findAllByGroupId(Long id) {
        return productGropMemberDao.findAllByGroupId(id);
    }

    @Override
    @Transactional
    public void updateProductIds(Long groupId, String productIds) {
        String[] productIdArray = productIds.split(",|，");
        productGropMemberDao.deleteByGroupId(groupId);
        for(String productId:productIdArray){
            ProductGropMember productGropMember = new ProductGropMember();
            productGropMember.setGroupId(groupId);
            productGropMember.setProductId(productId);
            productGropMember.setStatus("0");
            productGropMember.setCreateTime(new Date());
            jpaRepository.save(productGropMember);
        }
    }

    @Override
    @Transactional
    public void saveProductIds(String groupId, String productIds) {
        String[] idArray = productIds.split(",|，");
        ProductGroup productGroup=productGroupService.findById(Convert.toLong(groupId));
        if(productGroup != null){
            productGroup.setStatus("2");
            ProductGroup.putTimeAndUser(productGroup);
            productGroupService.save(productGroup);
            for(String productId:idArray){
                ProductGropMember productGropMember = new ProductGropMember();
                productGropMember.setGroupId(Convert.toLong(groupId));
                productGropMember.setProductId(productId);
                List<ProductGropMember> productGropMemberOpt=jpaRepository.findAll(Example.of(productGropMember));
                if(productGropMemberOpt!=null && productGropMemberOpt.size()>0){
                    continue;
                }
                productGropMember.setStatus("0");
                productGropMember.setCreateTime(new Date());
                jpaRepository.save(productGropMember);
            }
        }
    }

    @Override
    @Transactional
    public void delProductIds(String groupId, String productIds) {
        String[] idArray = productIds.split(",|，");
        ProductGroup productGroup=productGroupService.findById(Convert.toLong(groupId));
        if(productGroup != null){
            productGroup.setStatus("2");
            ProductGroup.putTimeAndUser(productGroup);
            productGroupService.save(productGroup);
            for(String productId:idArray){
                productGropMemberDao.deleteByGroupIdAndAndProductId(Convert.toLong(groupId),productId);
            }
        }
    }

    @Override
    public List<String> getProductIdsByGroupId(Long groupId) {
        List<String> list=productGropMemberDao.getProductIdsByGroupId(groupId);
        return list;
    }
}
